package com.mall.ibatis.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class JQGrid implements Serializable
{
	private Integer page; 
	private Integer total; /*total page*/
	private Integer record;
	private List<List<String>> row = new ArrayList<List<String>>();
	private List<ICell> rowXML = new ArrayList<ICell>();
	
	public Integer getPage()
	{
		return page;
	}
	public void setPage(Integer page)
	{
		this.page = page;
	}
	public Integer getTotal()
	{
		return total;
	}
	public void setTotal(Integer total)
	{
		this.total = total;
	}
	public Integer getRecord()
	{
		return record;
	}
	public void setRecord(Integer record)
	{
		this.record = record;
	}

	
	
	public List<List<String>> getRow()
	{
		return row;
	}
	public void setRow(List<List<String>> row)
	{
		this.row = row;
	}
	public String toXML()
	{
		//XStream xStream = new XStream();
		//xStream.alias("rows", this.getClass());
		//xStream.alias("cell", String.class);
		//return xStream.toXML(this);
		
		StringBuffer xmlString = new StringBuffer();
		xmlString.append("<?xml version='1.0' encoding='utf-8'?>");
		xmlString.append("<rows>");
		xmlString.append("<page>" + this.getPage()+"</page>");
		xmlString.append("<total>" + this.getTotal()+"</total>");
		xmlString.append("<records>" + this.getRecord()+"</records>");
		int index = 0;
		if(rowXML!=null && rowXML.size() >0)
		{
			for(ICell cell:rowXML)
			{
				if(cell.getCellID()!=null && cell.getCellID().length()!=0)
				{
					xmlString.append("<row id='"+cell.getCellID()+"'>");
				}
				else 
				{
					xmlString.append("<row id='"+(index++)+"'>");
				}
				
				xmlString.append(cell.toCell());
				xmlString.append("</row>");
			}
		}
		else 
		{
			for(List<String> r:row)
			{
				xmlString.append("<row id='"+(index++)+"'>");
				for(String cell:r)
				{
					xmlString.append("<cell><![CDATA["+cell+"]]></cell>");
				}
				xmlString.append("</row>");
			}
		}
		
		xmlString.append("</rows>");
		
		return xmlString.toString();
	}
	
	public String demo()
	{
		
		this.setPage(1);
		this.setTotal(1);
		this.setRecord(2);
		
		List<String> newRow = new ArrayList<String>();
		newRow.add("A1");
		newRow.add("A2");
		
		this.getRow().add(newRow);
		newRow = new ArrayList<String>();
		newRow.add("B1");
		newRow.add("B2");
		this.getRow().add(newRow);
		
		return this.toXML();
		
	}
	
	public static void main(String args[])
	{
		//System.out.println(new JQGrid().demo());
	}
	public List<ICell> getRowXML()
	{
		return rowXML;
	}
	public void setRowXML(List<ICell> rowXML)
	{
		this.rowXML = rowXML;
	}
	
	
}

